//
//  FavoritesViewController.m
//  CapriceMusic
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "FavoritesViewController.h"
#import "FavoritesTableViewCell.h"
#import "AppDelegate.h"

#define kHeight self.view.height / 5.2

@interface FavoritesViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *titleArr;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) DataBaseHandle *dataBase;


@end

@implementation FavoritesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收藏";
    
    self.titleArr = [NSMutableArray arrayWithCapacity:0];
    self.imageArr = [NSMutableArray arrayWithCapacity:0];
    [self willCheck];
    [self creationTableView];
    
    // Do any additional setup after loading the view.
}


////重写 - 系统自带编辑按钮的点击事件
//-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
//    [super setEditing:editing animated:animated];
//
//    //    让tableView处于可编辑/不可编辑状态
//    [self.tableView setEditing:editing animated:animated];
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.LeftSlideVC setPanEnabled:NO];
}

- (void)willCheck {
    self.dataBase = [DataBaseHandle shareDataBase];
    [self.dataBase openDataBase];
    NSMutableArray *dataSource = [self.dataBase selectDataBase];
    for (int i = 0; i < dataSource.count; i++) {
        NSDictionary *dic = dataSource[i];
        [self.titleArr addObject:dic[@"title"]];
        [self.imageArr addObject:dic[@"imageUrl"]];
    }
    [self.dataBase closedDataBase];
}

- (void)creationTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[FavoritesTableViewCell class] forCellReuseIdentifier:@"FavoritesTableViewCellIdentifier"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return kHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FavoritesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FavoritesTableViewCellIdentifier"];
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    [button setBackgroundImage:[UIImage imageNamed:@"rubbish"] forState:UIControlStateNormal];
//    button.frame = CGRectMake(self.view.width - 35, kHeight / 2, 30, 30);
//    [button addTarget:self action:@selector(deleteDidPress:) forControlEvents:UIControlEventTouchUpInside];
//    [cell.contentView addSubview:button];

    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[indexPath.row]]];
    cell.mytitle.text = self.titleArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    DetailViewController *detail = [[DetailViewController alloc] init];
    [self.dataBase openDataBase];
    NSMutableArray *dataSource = [self.dataBase selectDataBase];
    NSDictionary *dic = dataSource[indexPath.row];
    detail.appendingId = dic[@"ids"];
    detail.type = dic[@"type"];
    NSLog(@"%@", detail.type);
    [self.navigationController pushViewController:detail animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"不要删我";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataBase openDataBase];
        [self.dataBase deleteDataBase:self.titleArr[indexPath.row]];
        [self.dataBase closedDataBase];
        [self.imageArr removeObjectAtIndex:indexPath.row];
        [self.titleArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}

- (void)deleteDidPress:(UIButton *)sender {
    NSArray *visiblecells = [self.tableView visibleCells];
    for(UITableViewCell *cell in visiblecells) {
        NSLog(@"%ld", cell.tag);
//        [self.imageArr removeObjectAtIndex:cell.tag];
//        [self.titleArr removeObjectAtIndex:cell.tag];
//        [self.tableView reloadData];
        if(cell.tag == sender.tag) {
            [self.imageArr removeObjectAtIndex:cell.tag];
            [self.titleArr removeObjectAtIndex:cell.tag];
            [self.tableView reloadData];

            break;
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
